package com.sunyard.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sunyard.dal.bo.PaginationBO;
import com.sunyard.dal.bo.warning.WarningInfoQueryBO;
import com.sunyard.dal.dto.WarningInfoDTO;
import com.sunyard.dal.entity.Organization;
import com.sunyard.dal.mapper.OrganizationMapper;
import com.sunyard.dal.mapper.TerminalMapper;
import com.sunyard.manage.service.OrganizationService;
import com.sunyard.manage.service.WarningInfoService;
import com.sunyard.utils.DateUtil;
import com.sunyard.utils.Response.BaseResult;
import com.sunyard.utils.Response.GetResult;
import com.sunyard.utils.TokenUtil;
import com.sunyard.utils.manage.DataGrid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author wxl
 * @date 2022-07-11
 */
@Service
@Slf4j
public class WarningInfoServiceImpl implements WarningInfoService {

    @Autowired
    OrganizationMapper organizationMapper;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private TerminalMapper terminalMapper;

    /**
     * 预警信息超时时间
     */
    @Value("${WarningInfo.timeOut}")
    private long timeOut;

    @Override
    public BaseResult<DataGrid<WarningInfoDTO>> queryWarningInfo(PaginationBO<WarningInfoQueryBO> queryBO, HttpServletRequest request) {
        //long timeLong = 7 * 60 * 60 * 1000;
        String orgId = TokenUtil.getOrgId(request.getHeader("token"));
        WarningInfoQueryBO warningInfoQueryBO = queryBO.getParams();

        // List<String> orgIdList = terminalMapper.getKeyOrgidList();
        List<String> list = new ArrayList<>();
        //获取当前机构及所有子机构
        try {
            List<Organization> orgIds = organizationService.getOrgIds(orgId);
            for (Organization organization : orgIds) {
                list.add(organization.getOrgId());
            }
        } catch (Exception e) {
            log.error("获取当前机构及所有子机构异常", e);
            throw new RuntimeException(e.getMessage());
        }
        String lastTime = DateUtil.getDateStr(DateUtil.YYYYMMDD_HHMMSS, new Date());
        //未上传日志机构
        List<String> orgIdList = terminalMapper.getKeyOrgidList();
        List<String> orgKeyList = new ArrayList<>();
        for (String id : list) {
            for (String ids : orgIdList) {
                if (id.equals(ids)){
                    orgKeyList.add(ids);
                }
            }
        }
        log.info("====lastTime={}", System.currentTimeMillis());
        //根据当前时间 查询网点需要的数据
        long queryTime = DateUtil.getQueryTime();
        warningInfoQueryBO.setOrgIdList(orgKeyList);
        warningInfoQueryBO.setTimeOut(timeOut);
        warningInfoQueryBO.setCurrentTime(System.currentTimeMillis());
        warningInfoQueryBO.setEnd(queryTime);
        log.info("预警信息查询,请求参数[" + queryBO.getParams().toString() + "]");

        //设置分页信息
        // 执行查询
        List<WarningInfoDTO> warningInfoDTOList = new ArrayList<>();
        if (orgKeyList.size() > 0) {
            PageHelper.startPage(queryBO.getPageNo(), queryBO.getPageSize());
            warningInfoDTOList = organizationMapper.queryWarningInfo(warningInfoQueryBO);
        }
        // 取分页信息
        PageInfo<WarningInfoDTO> pageInfo = new PageInfo<>(warningInfoDTOList);
        // 设置返回结果
        DataGrid<WarningInfoDTO> dataGrid = new DataGrid<>();
        long total = pageInfo.getTotal();
        dataGrid.setTotal(new Long(total).intValue());
        dataGrid.setPageNum(queryBO.getPageNo());
        dataGrid.setRows(warningInfoDTOList);
        return GetResult.getResult(dataGrid);
    }
}